package com.winz.cultureapp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.CommunityDataAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.CommunityListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文化团体 -- 我的社团
 */
public class MyCommunityActivity extends BaseAppCompatActivity implements OnItemClickListener, XRecyclerView.LoadingListener {

    @BindView(R.id.tv_top_title)
    public TextView mTv_top_title;

    @BindView(R.id.xrcv_community)
    public XRecyclerView mXrcv_community;


    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;

    private List<CommunityListBean.DataBean.ListBean> mDatas;
    private CommunityDataAdapter mAdapter;

    @Override
    protected int contentView() {
        return R.layout.activity_community_my;
    }

    @Override
    protected void initViews() {
        mTv_top_title.setText("我的社团");
        mDatas = new ArrayList<>();

        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_community.setLayoutManager(manager);
        mXrcv_community.setPullRefreshEnabled(true);
        mXrcv_community.setLoadingMoreEnabled(true);

        mXrcv_community.setFocusable(false);
        mXrcv_community.setFocusableInTouchMode(false);

        mAdapter = new CommunityDataAdapter(this, mDatas, R.layout.empty_view, R.layout.item_community_list);
    }


    @Override
    protected void initEvents() {
        mXrcv_community.setAdapter(mAdapter);
        mXrcv_community.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void loadDatas(Intent intent) {

        getList();
    }

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();

        HttpUitls.onGetByToken("league_my",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                RequestFactory.getInstance().league_my(page + "",limit +""),
                new OnResponeListener<CommunityListBean>() {
            @Override
            public void onSuccess(CommunityListBean bean) {
                Logger.d("league_my==>>", JsonUtil.getJsonFromObj(bean));
                HttpUitls.isExpire(bean.code);
                if (bean.code == 0) {
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_community.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        CommunityListBean.DataBean.ListBean empty = new CommunityListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_community.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_community.refreshComplete();
                } else {
                    mXrcv_community.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                showMsg(error);
                if (isRefresh) {
                    mXrcv_community.refreshComplete();
                } else {
                    mXrcv_community.loadMoreComplete();
                }
            }
        });
    }

    @OnClick({R.id.fl_back})
    public void knife(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
        }
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        loadDatas(null);
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_community.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            loadDatas(null);
        }
    }

    /**
     * item点击事件
     *
     * @param parent
     * @param view
     * @param position
     */
    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.COMMUNITY.ITEM_ID,mDatas.get(position).leagueId);
        bundle.putString(Constants.KEY.COMMUNITY.FROM,"my");
        bundle.putString(Constants.KEY.COMMUNITY.ITEM_DES_HREF,Constants.URL.IP + "/league/detail/" + mDatas.get(position).leagueId);

        startActivity(CommunityDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
